<?php
/*----------------------------------------------------------------
	// Copyright (C) 2008-2010 itye.cn
	// 版权所有。
	//
	// 文件名：UpLoadFile.php
	// 文件功能描述：文件上传类
	//
	// 
	// 创建标识：
	//
	// 修改标识：
	// 修改描述：
//----------------------------------------------------------------*/
if (!defined('SHARE_INFO')) exit('No direct script access allowed');

class Images{
	public $FileDir	= 'uploads/';
	
	// 允许的文件类型
	public $AllowExt = array('jpg', 'gif', 'png', 'bmp');
	
	private $MkSubDirEd = false;
	
	public function __construct($FileDir = 'uploads/', $AllowExt=array('jpg', 'gif', 'png', 'bmp')){
		$this->AllowExt	= $AllowExt;
		$this->Set_FileDir($FileDir);
	}
	
	/*--------------------------------------
	 * Set UpLoad Dir
	 * @param string	$dir
	//-------------------------------------*/
	public function Set_FileDir($dir){
		if ( $this->MkSubDirEd ){
			return ;
		}

		$this->MkSubDirEd = true;

		if ( empty($dir) ){
			$this->UpLoadDir = __UPLOAD__;
		}else{
			$this->UpLoadDir = __UPLOAD__.$dir;
		}
		if ( !is_dir($this->FileDir) ){
			mkdir($this->FileDir, 0777);
			touch($this->FileDir.'index.htm');
		}
		$this->FileDir .= date('Ym'). '/';
		if(!is_dir($this->FileDir)) {
			mkdir($this->FileDir, 0777);
			touch($this->FileDir.'index.htm');
		}
	}
	
	/*--------------------------------------
	 * 取得文件扩展名
	 * @param string	$fileName
	//-------------------------------------*/
	public function GetExt($FileName) {
		return  strtolower(pathinfo($FileName, PATHINFO_EXTENSION));
	}
	
	/*--------------------------------------
	 * 保存远程图片到本地
	 * @param string	$dir
	//-------------------------------------*/
	public function SaveRemoteImg($body){
		$body = stripslashes($body);
		if ( !empty($body) ){
			$img_array = array();
			preg_match_all("/(src|SRC)=[\"|'|]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU", $body, $img_array);
			$img_array = array_unique($img_array[2]);
			set_time_limit(0);
			$imgPath = $this->FileDir;
			$FileName = strftime("%H%M%S", time());
			$imgUrl = str_replace(DATA, "", $imgPath).$FileName;
			foreach( $img_array as $key => $value ){
				$value = trim($value);
				$FileExt = $this->GetExt($value);
				$get_file = @file_get_contents($value);
				$rndFileName = $imgPath."/".$FileName.".".$FileExt;
				$fileurl = $imgUrl.$FileExt;
				if ( $get_file ){
					$fp = @fopen($rndFileName, "w");
					@fwrite($fp, $get_file);
					@fclose($fp);
				}
				$body = ereg_replace($value, $fileurl, $body);
			}
			$body = addslashes($body);
		}
		return stripslashes($body);
	}
	
	/*-----------------------------------------------
	 * 缩略图
	 * @param string	$SourceFile		原图片
	 * @param string	$SavePath		保存路径
	 * @param int		$ThumbWidth		缩略宽度
	 * @param int		$ThumbHeight	缩略高度
	 * @param int		$x				X轴坐标
	 * @param int		$y				Y轴坐标
	//---------------------------------------------*/
	public function CutImages($SourceFile = '', $SavePath = '', $ThumbWidth = '', $ThumbHeight = '', $CutTypes = 2, $SaveType = 1){
		if ( empty($ThumbWidth) ){
			$ThumbWidth	= 150;
		}
		if ( empty($ThumbHeight) ){
			$ThumbHeight = 150;
		}
		$img = @getimagesize($SourceFile);
		switch($img[2]){
			case 1:
				$im=@imagecreatefromgif($SourceFile);
			break;
			case 2:
				$im=@imagecreatefromjpeg($SourceFile);
			break;
			case 3:
				$im=@imagecreatefrompng($SourceFile);
			break;
		}
		list($ImgWidth, $ImgHeight) = $img;
		if ( empty($SavePath) ){
			$SavePath = pathinfo($SourceFile, PATHINFO_DIRNAME).'/';
		}
		$FileExt	= $this->GetExt($SourceFile);
		// 缩略图名称
		$ThumbName = basename($SourceFile,".".$FileExt).'_thumb.jpg';
		if ($CutTypes==1){
			// 按比例缩放
			$XRation = $ThumbWidth / $ImgWidth;
			$YRation = $ThumbHeight / $ImgHeight;
			if(($XRation * $ImgHeight) < $ThumbHeight) {
				$thumb['height'] = ceil($XRation * $ImgHeight);
				$thumb['width'] = $ThumbWidth;
			} else {
				$thumb['width'] = ceil($YRation * $ImgWidth);
				$thumb['height'] = $ThumbHeight;
			}
			$ThumbPhoto = imagecreatetruecolor($thumb['width'], $thumb['height']);
			/*
			 $thumbPhoto 输出目标图片
			 $upFilePhoto 要缩略的图片
			 $dst_x: 目標檔案開始點的 x 座標
			 $dst_y: 目標檔案開始點的 y 座標 
			 $src_x: 來源檔案開始點的 x 座標
			 $src_y: 來源檔案開始點的 y 座標
			 $dst_w: 目標檔案的長度
			 $dst_h: 目標檔案的高度
			 $src_w: 來源檔案的長度
			 $src_h: 來源檔案的高度
			*/
			imagecopyresampled($ThumbPhoto, $im ,0, 0, 0, 0, $thumb['width'], $thumb['height'], $ImgWidth, $ImgHeight);
			clearstatcache();
			if ( $SaveType == 1 ){
				imagejpeg($ThumbPhoto, $SavePath.$ThumbName,90);
			}else{
				imagejpeg($ThumbPhoto, $SourceFile,90);
			}
		}elseif ($CutTypes==2){
			// 按指定大小缩放
			$XRation = $ThumbWidth / $ImgWidth; //新的宽度与原图的比例
			$YRation = $ThumbHeight / $ImgHeight; // 缩略的高度与原图的比较
			if(($XRation * $ImgHeight) < $ThumbHeight) {
				// 说明比高的比例小于宽度,这时候高度不变，
				$thumb['height'] = $ThumbHeight;
				$thumb['width'] = ceil($YRation * $ImgWidth);
				$thumbtype = 'height';
			} else {
				$thumb['width'] = $ThumbWidth;
				$thumb['height'] = ceil($XRation * $ImgHeight);
				$thumbtype = 'width';
			}
			//坐标得到后，先生成缩略图，然后再由生成的缩略图上来计算坐标，最终生成符合要求的图片。
			$ThumbPhoto = imagecreatetruecolor($thumb['width'], $thumb['height']);
			imagecopyresampled($ThumbPhoto, $im, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $ImgWidth, $ImgHeight);
			clearstatcache();
			imagejpeg($ThumbPhoto, $SavePath.$ThumbName, 90);
			// 生成了缩略图片
			// 由生成的缩略图再次缩略
			$tmp_img = imagecreatefromjpeg($SavePath.$ThumbName);
			$o_width = imagesx($tmp_img);	//
			$o_height = imagesy($tmp_img);	//
			if ($thumbtype=='height'){
				$x=($o_width-$ThumbWidth)/2;
				$y = 0;
			}else{
				$x=0;
				$y=($o_height-$ThumbHeight)/2;
			}
			$thumbPhotox = imagecreatetruecolor($ThumbWidth, $ThumbHeight);
			imagecopyresampled($thumbPhotox, $tmp_img ,0, 0, $x, $y, $ThumbWidth, $ThumbHeight, $ThumbWidth, $ThumbHeight);
			clearstatcache();
			if ( $SaveType == 1 ){
				imagejpeg($ThumbPhoto, $SavePath.$ThumbName,90);
			}else{
				imagejpeg($ThumbPhoto, $SourceFile,90);
			}
		}
	}
}